package com.example.seh.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;

import com.example.seh.chat.Msg;
import com.example.seh.chat.MsgAdapter;
import com.example.seh.constant.Constant;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ChatUtil {
    public static List<Msg> msgList = new ArrayList<>();
    public static List<Msg> msgCache = new ArrayList<>();
    public static String youAvatar = "";
    public static String myAvatar = "";
    public static String getIPAddress(Context context) {
        //获取wifi下的IP地址并显示
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        String ip = intToIp(ipAddress);
        return ip;
    }
    //    用于将结果以IP的形式放出
    private static String intToIp(int i) {

        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

    public static void buildServer(Context context) {
        try {
            Log.e("chat","正在开启服务");
            ChatServer chatServer = new ChatServer();
            chatServer.start();

            Log.e("chat", "开启服务器成功");
            Log.e("chat","我的ip地址是：" + ChatUtil.getIPAddress(context));
        } catch (IOException e) {
            Log.e("chat", "开启服务器失败");
            e.printStackTrace();
        }
    }


}
